package service

import (
	. "cloudmusic/config"
	"context"
	"fmt"
	"github.com/minio/minio-go/v6"
	"io"
	"log"
)

type FileIO struct {
	ObjectName  string
	FileReader  io.Reader
	ContentType string
	BucketName  string //类型
	Size        int64
	FileUrl     string
	Type        int8 //0是FileReader 1是FileUrl
}

var MinIOClient *minio.Client
var FileUploadChan = make(chan FileIO, 20)
var location = "us-east-1"

func init() {
	var err error
	MinIOClient, err = minio.New(EndPoint, AccessKeyID, SecretAccessKey, UseSSL)
	if err != nil {
		log.Fatalln(err)
	}
}
func FileUploader() {
	for {
		select {
		case file := <-FileUploadChan:
			_ = MinIOClient.MakeBucket(file.BucketName, location)
			switch file.Type {
			case 0:
				_, err := MinIOClient.PutObjectWithContext(context.Background(), file.BucketName, file.ObjectName, file.FileReader, file.Size, minio.PutObjectOptions{ContentType: file.ContentType})
				if err != nil {
					fmt.Println("err:", err)
				}
			case 1:
				_, err := MinIOClient.FPutObject(file.BucketName, file.ObjectName, file.FileUrl, minio.PutObjectOptions{ContentType: file.ContentType})
				if err != nil {
					fmt.Println("err:", err)
				}
			}

		}

	}
}
